<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Student;
use App\Models\TeachingSimulate;
use App\Repositories\StudentCountRepository;
use App\Repositories\TeachingRepository;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Repositories\OssRepository;

class TeachingController extends BaseController
{
    protected $student_count;
    protected $teaching;
    protected $oss;

    public function __construct(
        StudentCountRepository $student_count,
        TeachingRepository $teachingRepository,
        OssRepository $oss

    )
    {
        $this->student_count = $student_count;
        $this->teaching = $teachingRepository;
        $this->oss = $oss;
    }


    /**
     * @api {get} /teaching/exchange 用户问题列表
     * @apiDescription 用户问题列表
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/exchange
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [page=1]  第N页
     * @apiParam {Number} [size=10]  每页N条
     * @apiParam {Number} [type=1]  1. 全部提问 2.我的提问
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "status_code": 200,
     *       "data":[
     *        {
     *           "id": "1",
     *           "user_id": "1",
     *           "text": "XZXXXXXXXXX",
     *           "type":1,
     *           "created_at":"2018-06-06 17:41:18",
     *           "name": "谢兴",
     *           "photo": "http://image.stjp360.com/jishi/4373360266622780/stuimg/photo/2017/10/12/1507778553JZc.jpg"
     *           "count": 0  //回复条数,
     *       },
     *       {
     *           "id": "2",
     *           "user_id": "1",
     *           "text": "XZXXXXXXXXX",
     *           "type":1,
     *           "created_at":"2018-06-06 17:41:18",
     *           "name": "谢兴",
     *           "photo": "http://image.stjp360.com/jishi/4373360266622780/stuimg/photo/2017/10/12/1507778553JZc.jpg"
     *           "count": 1  //回复条数,
     *      }
     *     ]
     *   }
     */
    public function exchange(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }
        $size = $request->get('size', 10);

        $param = [
            'page' => $request->get('page', 1)
        ];

        $id = '';
        if ($request->get('type') == 2) {
            $id = $user->item->id;
        }
        $data = $this->teaching->exchangeList($param, $id, $size);

        return $this->successResponse('success', ['data' => $data]);
    }

    /**
     * @api {post} /teaching/exchangeDetail 用户问题详情
     * @apiDescription 用户问答详情
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/exchangeDetail
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [id]  问题ID
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *      {"message":"success",
     *       "code":200,
     *       "data":{
     *            "info": {
     *              "id":2,
     *              "user_id":15241,
     *              "type":1,
     *              "text":"cesi",
     *              "created_at":"2018-06-06 17:41:18",
     *              "name": "谢兴",
     *              "photo": "http://image.stjp360.com/jishi/4373360266622780/stuimg/photo/2017/10/12/1507778553JZc.jpg"
     *          },
     *       "list":[
     *          {
     *              "id":3,
     *              "user_id":15241,
     *              "type":2,
     *              "text":"huifu",
     *              "created_at":"2018-06-06 17:41:18",
     *              "name": "谢兴",
     *              "photo": "http://image.stjp360.com/jishi/4373360266622780/stuimg/photo/2017/10/12/1507778553JZc.jpg"
     *          },
     *          {
     *              "id":5,
     *              "user_id":15241,
     *              "type":2,
     * `            "text":"\u56de\u590d",
     *              "created_at":"2018-06-06 17:42:45",
     *              "name": "谢兴",
     *              "photo": "http://image.stjp360.com/jishi/4373360266622780/stuimg/photo/2017/10/12/1507778553JZc.jpg"
     *          }
     *        ]
     *      }
     *   }
     *
     *
     */
    public function exchangeDetail(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        if (!$request->get('id')) {
            return $this->errorResponse('参数错误');
        }

        $data = $this->teaching->exchangeDetail($request->get('id'), $user->item);

        return $this->successResponse('success', ['data' => $data]);

    }

    /**
     * @api {post} /teaching/exchangeAdd 用户提交问题
     * @apiDescription 用户提交问题
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/exchangeAdd
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Text} [text]  内容
     * @apiParam {Number} [id]  问题ID
     * @apiParam {Number} [type=1]  类型 1.提问 2.回答 (默认为1)
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "success",
     *       "code": 200
     *   }
     */
    public function exchangeAdd(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        if (!$request->get('text')) {
            return $this->errorResponse('请输入你要提的问题');
        }

        if ($request->get('type') == 2) {
            $rs = $this->teaching->insert($request->get('text'), $user->item, 2, $request->get('id'));
        } else {
            $rs = $this->teaching->insert($request->get('text'), $user->item, 1);
        }

        if ($rs) {
            return $this->successResponse('success');
        } else {
            return $this->errorResponse('操作失败');
        }
    }

    /**
     * @api {get} /teaching/curriculum 用户课程列表
     * @apiDescription 用户课程列表
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/curriculum
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [page=1]  第N页
     * @apiParam {Number} [size=10]  每页N条
     * @apiParam {Number} [km=1]  1. 科目一  4.科目四
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "status_code": 200,
     *       "data":[
     *        {
     *           "id": "1",
     *           "name": "测试",
     *           "courseware_id": "1,2",  //课件ID
     *           "study_time":"1",  //培训学时
     *           "type":"1",     //课程计费类型  1 免费课程 2 计费课程
     *           "access": "1",  //访问权限 1 所有用户 2 付费访问
     *           "money": "0.00"   //计费标准(元)
     *       },
     *       {
     *           "id": "2",
     *           "name": "测试课程",
     *           "courseware_id": "",  //课件ID
     *           "study_time":"4",  //培训学时
     *           "type":"1",     //课程计费类型  1 免费课程 2 计费课程
     *           "access": "1",  //访问权限 1 所有用户 2 付费访问
     *           "money": "0.00"   //计费标准(元)
     *      }
     *     ]
     *   }
     */
    public function curriculum(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $size = $request->get('size', 10);

        $param = [
            'page' => $request->get('page', 1),
            'km' => $request->get('km', 1),
        ];

        $rs = $this->teaching->curriculumList($param, $size, $user->item);

        return $this->successResponse('success', ['data' => $rs]);
    }

    /**
     * @api {post} /teaching/courseware 用户课件列表
     * @apiDescription 用户课件列表
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/courseware
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [page]  第N页
     * @apiParam {Number} [size]  每页N条
     * @apiParam {Number} [curriculum_id]  课程ID
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "status_code": 200,
     *       "data":[
     *        {
     *           "id": "1",
     *           "name": "测试1",
     *           "url": "http://image.stjp360.com/jishi/teaching/2018/06/04/1528097032R6j.MOV", //课件附件
     *           "courseware_time":"20",  //课件时长
     *           "type":"1",     //课程计费类型  1 免费课程 2 计费课程
     *           "access": "1",  //访问权限 1 所有用户 2 付费访问
     *           "money": "3.00"   //计费标准(元)
     *           "study_pace": "2"   //学习进度
     *           "second": "120"   //视频时间(秒)
     *           "assessment_id": "7"   //评论ID
     *           "is_watch": 1  //视频是否学习 1 为已学习
     *       },
     *       {
     *           "id": "2",
     *           "name": "测试2",
     *           "url": null,  //课件附件
     *           "courseware_time":"20",  //课件时长
     *           "type":"1",     //课程计费类型  1 免费课程 2 计费课程
     *           "access": "1",  //访问权限 1 所有用户 2 付费访问
     *           "money": "3.00"   //计费标准(元)
     *           "study_pace": null   //学习进度
     *           "second": 0   //视频时间
     *           "assessment_id": null   //评论ID
     *           "is_watch": null  //视频是否学习 1 为已学习
     *      }
     *     ]
     *   }
     */
    public function courseware(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $rs = $this->teaching->isPay($request->get('curriculum_id'), $user->item);
        if ($rs) {
            return $this->errorResponse('尚未购买该课程');
        }

        $size = $request->get('size', 10);
        $page = $request->get('page', 1);

        $res = $this->teaching->coursewareList($request->get('curriculum_id'), $user->item, $size, $page);

        return $this->successResponse('success', ['data' => $res]);
    }

    /**
     * @api {get} /teaching/assessment 评论详情
     * @apiDescription 评论详情
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/assessment
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [id]  评论ID
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "status_code": 200,
     *       "data":[
     *        {
     *           "id": "1",
     *           "assessment_content": "1", //课程内容
     *           "assessment_content": "2", //教学方法
     *           "assessment_content":"3",  //教学效果
     *           "assessment_content":"1",     //服务态度
     *           "service_capacity": "2",  //服务能力
     *           "service_efficiency": "3"   //服务效率
     *           "text": "评论"   //内容
     *       }
     *     ]
     *   }
     */
    public function assessment(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        if ($request->get('id')) {
            $id = $request->get('id');
        } else {
            return $this->errorResponse('参数错误');
        }

        $rs = $this->teaching->assessment($id);

        return $this->successResponse('success', ['data' => $rs]);
    }

    /**
     * @api {post} /teaching/assessmentAdd 用户提交评论
     * @apiDescription 用户提交评论
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/assessmentAdd
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [courseware_id]  课件ID
     * @apiParam {Number} [assessment_content]  课件内容 评价 1-5
     * @apiParam {Number} [assessment_method]  教学方法 1-5
     * @apiParam {Number} [assessment_result]  教学效果 1-5
     * @apiParam {Number} [service_attitude]  服务态度 1-5
     * @apiParam {Number} [service_capacity]  服务能力 1-5
     * @apiParam {Number} [service_efficiency]  服务效率 1-5
     * @apiParam {Text} [text]  内容
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "success",
     *       "code": 200
     *   }
     */
    public function assessmentAdd(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = [
            'courseware_id' => $request->get('courseware_id'),
            'assessment_content' => $request->get('assessment_content'),
            'assessment_method' => $request->get('assessment_method'),
            'assessment_result' => $request->get('assessment_result'),
            'service_attitude' => $request->get('service_attitude'),
            'service_capacity' => $request->get('service_capacity'),
            'service_efficiency' => $request->get('service_efficiency'),
        ];

        if(!($param['assessment_content'] && in_array($param['assessment_content'],[1,2,3,4,5]))){
            return $this->errorResponse('参数错误');
        }
        if(!($param['assessment_content'] && in_array($param['assessment_method'],[1,2,3,4,5]))){
            return $this->errorResponse('参数错误');
        }
        if(!($param['assessment_content'] && in_array($param['assessment_result'],[1,2,3,4,5]))){
            return $this->errorResponse('参数错误');
        }
        if(!($param['assessment_content'] && in_array($param['service_attitude'],[1,2,3,4,5]))){
            return $this->errorResponse('参数错误');
        }
        if(!($param['assessment_content'] && in_array($param['service_capacity'],[1,2,3,4,5]))){
            return $this->errorResponse('参数错误');
        }
        if(!($param['assessment_content'] && in_array($param['service_efficiency'],[1,2,3,4,5]))){
            return $this->errorResponse('参数错误');
        }

        if ($request->get('text')) {
            $param['text'] = $request->get('text');
        }

        $rs = $this->teaching->assessmentAdd($param, $user->item);

        if ($rs) {
            return $this->successResponse('success');
        } else {
            return $this->errorResponse('评论失败');
        }
    }

    /**
     * @api {post} /teaching/studyPace 用户学习进度更新
     * @apiDescription 用户学习进度更新
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/studyPace
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [courseware_id]  课件ID
     * @apiParam {Number} [second]  秒
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "success",
     *       "code": 200
     *   }
     */
    public function studyPace(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = [
            'courseware_id' => $request->get('courseware_id'),
            'second' => $request->get('second'),
        ];

        $rs = $this->teaching->studyPaceUpdate($param, $user->item);

        if ($rs) {
            return $this->successResponse('success');
        } else {
            return $this->errorResponse('学习进度更新失败');
        }
    }

    /**
     * @api {get} /teaching/detail 理论测试-模拟考试题目详情
     * @apiDescription 模拟考试题目详情
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/detail
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [id]  传入题目的ID
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "code": 200,
     *       "data": {
     *          "id": 1599,
     *          "title": "考试题目 -----例如 夜间驾驶机动车在窄路、窄桥会车怎样使用灯光？",
     *          "url": "http://www.exc360.com/attachment/file/drive/questions/2016/03/30/5eb4d75agw1e28yntci5ej.jpg", //地址
     *          "train_type": "A1,A2,A3,B1,B2,C1,C2,C3,C4,C5,D,E,M,F,N,P",
     *          "teaching_type": "5",
     *          "answer": [
     *              "A、违规行为",
     *              "B、违章行为",
     *              "C、违法行为",
     *              "D、犯罪行为"
     *              ],          //试题的答案
     *          "answerT": "C",//正确答案
     *          "pid": 1,
     *          "mid": 5,
     *          "status": "1",
     *          "type": "1", 1为单选，2为多选，3为判断
     *          "sid": 1,
     *          "km": 1,    //科目
     *          "content": "答案解析 ----《道路交通安全法实施条例》第四十八条：在没有中心隔离设施或者没有中心线的道路上，夜间会车应当在距相对方向来车150米以外改用近光灯，在窄路、窄桥与非机动车会车时应当使用近光灯。",
     *          "ydt_id": 132
     *
     *       }
     *   }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function detail(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        if(!$id = $request->get('id')){
            return $this->errorResponse('参数错误');
        }

        $data['question'] = $this->teaching->getById($id);

        $data['question']['answer'] = explode('||', $data['question']['answer']);
        if ($data['question']['url']) {
            if (!strpos($data['question']['url'],'http')) {
                $data['question']['url'] = 'http://www.exc360.com'.$data['question']['url'];
            }
        }

        return $this->successResponse('success',['data'=>$data]);
    }

    /**
     * @api {get} /teaching/simulate 理论测试-模拟考试
     * @apiDescription 模拟考试
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/simulate
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [km]  1为科目一考试 4为科目四考试
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "code": 200,
     *       "data": {
     *          "question": {
     *              "id": 1872,
     *              "title": "变更车道时只需开启转向灯，并迅速转向驶入相应的车道，以不妨碍同车道机动车正常行驶。",
     *              "url": "",
     *              "train_type": "A1,A2,A3,B1,B2,C1,C2,C3,C4,C5,D,E,M,F,N,P",
     *              "teaching_type": "5",
     *              "answer": [
     *                  "A、正确",
     *                  "B、错误"
     *              ],
     *              "answerT": "B",
     *              "pid": 1,
     *              "mid": 5,
     *              "status": 1,
     *              "type": 1,
     *              "km": 1,
     *              "sid": 1,
     *              "content": "经常跟大家说，看到“只需”等太绝对的词，及“迅速”等不利安全的词，基本上都选错误。",
     *              "ydt_id": 2580,
     *              "titleUrl": null
     *          },
     *          "simulate": [
     *          {
     *            "id": 1872
     *          },
     *          {
     *             "id": 1488
     *          },
     *          {
     *            "id": 1998
     *          },
     *          {
     *            "id": 1710
     *          },
     *          {
     *             "id": 2444
     *          },
     *          {
     *              "id": 1854
     *           },
     *          {
     *               "id": 2178
     *          }
     *          ]
     *      }
     *   }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function simulate(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token


        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = [
            'km' => $request->get('km'),//模拟测试的科目类型1,2
        ];
        if(!($param['km'] && in_array($param['km'],[1,4]))){
            return $this->errorResponse('参数错误');
        }

        $data = $this->teaching->getSimulate($param['km'], $user->item);


        return $this->successResponse('success',['data'=>$data]);
    }

    /**
     * @api {get} /teaching/getChapterList 理论测试-章节列表
     * @apiDescription 章节列表
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/getChapterList
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [km]  类型  1为科目一的章节列表 4为科目四的章节列表
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "code": 200,
     *       "data":[
     *          {
     *            "id": 3, //章节ID
     *            "name": "1.1机动车驾驶证申领与使用" //章节名称
     *            "km": 1 //科目
     *          },
     *          {
     *            "id": 10, //章节ID
     *            "name": "1.2 测试车辆" //章节名称
     *            "km": 1 //科目
     *         }
     *      ]
     *   }
     */
    public function chapterList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = [
            'km' => $request->get('km'),
        ];

        if(!($param['km'] && in_array($param['km'],[1,4]))){
            return $this->errorResponse('参数错误');
        }

        $data = $this->teaching->getChapterList($param, $user->item);

        return $this->successResponse('success', ['data'=>$data]);
    }

    /**
     * @api {get} /teaching/chapterLearn 理论测试-章节列表题目
     * @apiDescription 章节列表题目
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/chapterLearn
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [pid]  章节ID
     * @apiParam {Number} [page] 第几题
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "code": 200,
     *       "data": {
     *           "list": [
     *           {
     *              "id": 1470,
     *              "title": "机动车驾驶人造成事故后逃逸构成犯罪的，吊销驾驶证且多长时间不得重新取得驾驶证？",
     *              "url": "",
     *              "train_type": "A1,A2,A3,B1,B2,C1,C2,C3,C4,C5,D,E,M,F,N,P",
     *              "teaching_type": "5",
     *              "answer": [
     *                  "A、5年内",
     *                  "B、10年内",
     *                  "C、终生",
     *                  "D、20年内"
     *              ],
     *              "answerT": "C",
     *              "pid": 1,
     *              "mid": 5,
     *              "status": 1,
     *              "type": 3,
     *              "km": 1,
     *              "sid": 1,
     *              "content": "《道路交通安全法》第一百零一条：造成交通事故后逃逸的，由公安机关交通管理部门吊销机动车驾驶证，且终生不得重新取得机动车驾驶证。",
     *              "ydt_id": 3
     *              }
     *          ],
     *          "count": 1000
     *   }
     */
    public function chapterLearn(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = $request->input();

        if(!$param['pid'] || !$param['page']){
            return $this->errorResponse('参数错误');
        }

        $data = $this->teaching->getChapterLearn($param);

        return $this->successResponse('success',['data'=>$data]);
    }

    /**
     * @api {get} /teaching/learnRandom 理论测试-章节顺序(随机)
     * @apiDescription 章节顺序(随机)
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/learnRandom
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [page] 第几题
     * @apiParam {Number} [km] 科目
     * @apiParam {Number} [order_type] 1.顺序 2.随机
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "code": 200,
     *       "data": {
     *           "list": [
     *           {
     *              "id": 1470,
     *              "title": "机动车驾驶人造成事故后逃逸构成犯罪的，吊销驾驶证且多长时间不得重新取得驾驶证？",
     *              "url": "",
     *              "train_type": "A1,A2,A3,B1,B2,C1,C2,C3,C4,C5,D,E,M,F,N,P",
     *              "teaching_type": "5",
     *              "answer": [
     *                  "A、5年内",
     *                  "B、10年内",
     *                  "C、终生",
     *                  "D、20年内"
     *              ],
     *              "answerT": "C",
     *              "pid": 1,
     *              "mid": 5,
     *              "status": 1,
     *              "type": 3,
     *              "km": 1,
     *              "sid": 1,
     *              "content": "《道路交通安全法》第一百零一条：造成交通事故后逃逸的，由公安机关交通管理部门吊销机动车驾驶证，且终生不得重新取得机动车驾驶证。",
     *              "ydt_id": 3
     *              }
     *          ],
     *          "count": 1000
     *   }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function learnRandom(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = $request->input();

        if(!$param['page']){
            return $this->errorResponse('参数错误');
        }

        $data = $this->teaching->learnList($param, $user->item);

        return $this->successResponse('success',['data'=>$data]);
    }

    /**
     * @api {get} /teaching/getWrongList 理论测试-错题列表
     * @apiDescription 错题列表
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/practice/getWrongList
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [km] 1.科目一  4.科目四
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "",
     *       "code": 200,
     *       "data": {
     *          "question": {
     *              "id": 2000,
     *              "title": "驾驶机动车遇前方交叉路口交通阻塞时，路口内无网状线的，可停在路口内等候。",
     *              "url": "",
     *              "train_type": "A1,A2,A3,B1,B2,C1,C2,C3,C4,C5,D,E,M,F,N,P",
     *              "teaching_type": "5",
     *              "answer": [
     *                  "A、正确",
     *                  "B、错误"
     *              ],
     *              "answerT": "B",
     *              "pid": 1,
     *              "mid": 5,
     *              "status": 1,
     *              "type": 1,
     *              "km": 1,
     *              "sid": 1,
     *              "content": "《中华人民共和国道路交通安全法实施条例》第五十三条：机动车遇有前方交叉路口交通阻塞时，应当依次停在路口以外等候，不得进入路口。",
     *              "ydt_id": 11028
     *          },
     *          "simulate": [
     *              {
     *                  "simulate_id": 2000
     *              }
     *          ]
     *      }
     *   }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function getWrongList(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token


        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = [
            'km' => $request->get('km'),
        ];

        if(!($param['km'] && in_array($param['km'],[1,4]))){
            return $this->errorResponse('参数错误');
        }

        $data = $this->teaching->getWrongList($param['km'], $user->item);


        return $this->successResponse('success',['data'=>$data]);
    }

    /**
     * @api {post} /teaching/wrongUpdate 理论测试-错题更新
     * @apiDescription 错题更新
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/wrongUpdate
     * @apiHeader {String} Authorization 用户旧的jwt-token, value 以 Bearer 开头
     * @apiParam {Number} [km]  1.科目一  4.科目4
     * @apiParam {Number} [simulate_id]  试题ID
     * @apiParam {Number} [is_delete]  是否删除 1 删除 (删除时传值)
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "success",
     *       "code": 200
     *   }
     */
    public function wrongUpdate(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if (!$this->checkToken($user->item, JWTAuth::getToken())) {
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        if(!$request->get('simulate_id')){
            return $this->errorResponse('参数错误');
        }

        $param = [
            'simulate_id' => $request->get('simulate_id'),
            'km' => $request->get('km'),
            'is_delete' => $request->get('is_delete'),
        ];

        $rs = $this->teaching->wrongUpdate($param, $user->item);

        if ($rs) {
            return $this->successResponse('success');
        } else {
            return $this->errorResponse('错题更新失败');
        }
    }

    /**
     * @api {post} /teaching/theoryPhoto 理论测试-拍照
     * @apiDescription 理论测试/拍照
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/theoryPhoto
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [courseware_id]  课件ID
     * @apiParam {image} [image]  base64编码的图片内容
     * @apiParam {Number} [type]  1表示看前拍照，2表示看完拍照
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "上传成功",
     *       "code": 200,
     *   }
     */
    public function theoryPhoto(Request $request)
    {

        $user = JWTAuth::parseToken()->authenticate();//验证token


        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $image = $request->input('image');
        $courseware_id = $request->input('courseware_id');
        $type = $request->input('type');

        if($image){
            $file_name = 'teaching_photo/'.$user->item->id.'_'.$courseware_id.'_'.$type.'.jpg';
            $rs = $this->oss->uploadContentToFile(base64_decode($image), $file_name);

            if ($rs['state']) {
                return $this->successResponse('上传成功');
            }
        }
        return $this->errorResponse('上传失败');
    }

    /**
     * @api {post} /teaching/theoryRecord 理论测试-视频学习记录
     * @apiDescription 视频学习记录
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/theoryRecord
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiParam {Number} [courseware_id]  学习的视频ID
     * @apiParam {Number} [km]  1.科目一 4.科目四
     * @apiParam {Number} [duration]  学习的视频时长
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "success",
     *       "code": 200,
     *   }
     */
    public function theoryRecord(Request $request)
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $param = [
            'courseware_id' => $request->input('courseware_id'),
            'duration' => $request->input('duration'),
            'km' => $request->input('km'),
        ];

        if(!$param['courseware_id']){
            return $this->errorResponse('参数错误');
        }

        $param['student_id'] = $user->item_id;
        $param['school_id'] = Student::find($user->item_id)->school_id;

        $rs = $this->teaching->theoryRecord($param, '');
        return $this->successResponse('success');
    }

    /**
     * @api {post} /teaching/getSet 教学设置
     * @apiDescription 教学设置
     * @apiGroup Teaching
     * @apiPermission none
     * @apiSampleRequest /api/teaching/theoryRecord
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *   {
     *       "message": "success",
     *       "code": 200,
     *       "data": {
     *           "idcard_time": 15,  //身份验证最大时长(分钟
     *           "interaction_time": 4 //交互间隔最大时长(秒
     *      }
     *   }
     */
    public function getSet()
    {
        $user = JWTAuth::parseToken()->authenticate();//验证token

        if(!$this->checkToken($user->item, JWTAuth::getToken())){
            return $this->errorResponse('你的账号在其他地方登录', 401);
        }

        $data = $this->teaching->getSet();
        return $this->successResponse('success',['data'=>$data]);
    }

}
